import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/* Authors:  Andrew Sytsma and Alex Kraska
 * 
 * Event occurs when the button is clicked.
 * If the user guesses the correct number of circles, which is entered in the text field,
 * the program displays a success message.  If the user guesses incorrectly, the program
 * displays a failure message.
 */
public class ButtonListener extends JButton implements ActionListener
{
	//declare variables to receive the textfield containing the guess
	//and to hold the actual number of circles that exist
	private JTextField textField;
	private int actualCount;
	private Dimension size;
	private boolean guessed;
	private TimerMan timer;
	
	public ButtonListener(JTextField tField, int aCount, Dimension s, TimerMan t)
	{
		super("Enter Guess");
		this.addActionListener(this);
		this.size = s;
		textField = tField;
		actualCount = aCount;
		this.guessed = false;
		this.timer = t;
	}	//end of default constructor
	
	public void actionPerformed(ActionEvent actionEvent)
	{
		//declare variable to hold the how many circles the user believes
		//are on the screen
		int userCount = 0;
		
		//attempts to parse the input to an Integer
		//an error message is thrown if the input is in an incorrect format
		try
		{
			userCount = Integer.parseInt(textField.getText());
		}
		catch(NumberFormatException ex)
		{
			JOptionPane.showMessageDialog(null, "The input must be a valid integer");
		}	//end try catch
		
		//checks to make sure that the user entered a positive integer
		if(userCount > 0)
		{
			this.timer.stopTimer();
			
			//checks to see if the user guessed the correct number of circles
			//and displays a success or failure message corresponding to the results
			if(userCount == actualCount)
				JOptionPane.showMessageDialog(null, "Congratulations!  You guessed the correct number of circles!");
			else
				JOptionPane.showMessageDialog(null, "YOU FAILURE!  WHAT WOULD YOUR MOTHER SAY IF SHE SAW YOUR RESULTS?!?!");
			//end if
			
			this.guessed = true;
		}
		else
			JOptionPane.showMessageDialog(null, "The input must be a positive integer");
		//end if
	}	//end of actionPerformed method
	
	public Dimension getPreferredSize()
	{
		return this.size;
	}
	
	public boolean hasGuessed()
	{
		return this.guessed;
	}
}	//end of ButtonListener class